/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * HalfSizeValidator.java, 10/1/2013, bacdv
 */
package net.luvina.manageuser.validator;

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.luvina.manageuser.util.Message;

/**
 * HalfSizeValidator
 *
 * @author bacdv
 *
 */
public class HalfSizeValidator implements Validator {

    /*
     * (non-Javadoc)
     *
     * @see
     * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext
     * , javax.faces.component.UIComponent, java.lang.Object)
     */
    public void validate(FacesContext context, UIComponent component, Object obj)
            throws ValidatorException {
        String halfSize = (String) obj;
        String labelItem = (String) component.getAttributes().get("label");
        String pattern = "[0-9]+";
        Pattern patt = Pattern.compile(pattern);
        Matcher match = patt.matcher(halfSize);
        boolean matchFound = match.find();
        if (!matchFound) {
            FacesMessage message = new FacesMessage();
            message.setSummary(MessageFormat.format(Message.getString("ER018"),
                    labelItem));
            throw new ValidatorException(message);
        }
    }

}
